<?php
/**
* Copyright 2009 Waag Society <http://www.waag.org>
* This file is part of Annotrax.
*
* Annotrax is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Annotrax is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Annotrax.
* If not, see http://www.gnu.org/licenses/gpl.html
*/

?>
<?php
class Default_Form_Annotate extends Zend_Form{
    private $channel;
    private $annotation;

    public function init(){
        $this->setMethod('post');
    }

    public function setChannel( Default_Model_Channel $channel ){
        $this->channel = $channel;
        $this->createAnnotationForm();
    }

    private function getChannel(){
		if( null == $this->channel && isset( $this->annotation ) ){
			$this->channel = new Default_Model_Channel( $this->annotation->channelId );
		}
        return $this->channel;
    }

    public function setAnnotation( Default_Model_Annotation $annotation ){
        $this->annotation = $annotation;
//        $this->setChannel( new Default_Model_Channel( $annotation->channelId ) );
        $this->populateValues();
    }

    private function getAnnotation(){
        return $this->annotation;
    }

    private function populateValues(){
        $config = $this->getChannel()->getConfig();

        if( $config->multi || $config->single ){
            $element = $this->getElement( 'keywords' );

			$keywords = (array) $this->getAnnotation()->getKeywords();

            $ids = array_map(create_function( '$value', 'return $value->id;' ), $keywords );

            $element->setValue( $ids );

        }

        if( $config->comment ){
            $element = $this->getElement('comment');
            $element->setValue( $this->getAnnotation()->comment );
        }

        $this->getElement('duration')
             ->setValue( $this->getAnnotation()->getDuration() );

        $this->getElement('start')
             ->setValue( $this->getAnnotation()->getStart() );
    }

    private function createAnnotationForm(){
        $channel = $this->getChannel();
        $config  = $channel->getConfig();

        $e = new Zend_Form_Element_Text('start', array(
            'label'     	=> 'Start:',
            'value'     	=> '00:00:00',
            'required'  	=> true,
			'ErrorMessages' => array('Start must be a valid timecode or integer'),
			'validators'	=> array(array('Regex', true, array('/^([0-9][0-9]:[0-5][0-9]:[0-5][0-9])$|^(\d+)$|^(\d*[1-9]\d*)$/')))
        ));
        $this->addElement( $e );

		$this->addElement( 'text', 'duration', array(
            'label'    		=> 'Duration:',
            'value'     	=> '00:00:10',
            'required'  	=> true,
			'ErrorMessages' => array('Duration must be a valid timecode or non-zero integer'),
			'validators'	=> array(array('Regex', true, array('/^(([0-5][0-9]:){2}[0-5]((?<!00:00:0)[0-9]|[1-9]))$|^(\d*[1-9]\d*)$/')))
		));

        $this->addDisplayGroup( array('start', 'duration'), 'startDuration', array());

        if( $config->comment ){
			$this->addElement( 'textarea', 'comment', array(
				'label'			=> null,
				'value'			=> null,
				'required'		=> true,
				'validators' 	=> array(array('NotEmpty', true)),
				'ErrorMessages' => array('Comments cannot be empty')
			));
        }

        if( $config->single || $config->multi ){
			if( $config->open ){
				$this->addElement( 'text', 'addKeywords', array(
					'label'		=> "You can add new keywords here, comma-separated",
					'filters'	=> array('stringTrim')
				));
			}
            if( $config->single ){
				$this->addElement( 'Radio', 'keywords', array(
					'required' => true,
					'validators'	=> array(array('NotEmpty', true))
				));
            }
            else{
				$this->addElement( 'MultiCheckbox', 'keywords', array(
					'required' => true,
					'validators'	=> array(array('NotEmpty', true))
				));
            }

			$element = $this->getElement( 'keywords' );

            foreach( $channel->getKeywords() as $keyword ){
                $element->addMultiOption( $keyword->id, $keyword->name   );
            }

//            $this->addElement( $element );
            $this->addDisplayGroup( array( 'keywords' ), 'keywordsGroup', array(
                'legend'    => 'Keywords'
            ));
        }

        $this->addElement( 'submit', 'save', array(
            'label' => 'save annotation'
        ));
        $this->addElement( 'submit', 'cancel', array(
            'label' => 'cancel editing'
        ));

        $this->addDisplayGroup( array('save', 'cancel'), 'buttonBar' );

        $groups = $this->getDisplayGroups();

        foreach( $groups as $group ){
            $group->removeDecorator('DtDdWrapper');
            $group->removeDecorator('HtmlTag');
            //$group->setAttrib('class', $group->getId() );

            $fieldset = $group->getDecorator( 'Fieldset' );
            $fieldset->setOption( 'class', $group->getId() );
        }

        $elements = $this->getElements();

        foreach( $elements as $element ){
            $id = $element->getId();
            $element->setAttrib( 'class', 'input-' . $id );

            $element->removeDecorator( 'DtDdWrapper' );
            $tag = $element->getDecorator('Label');


            if( is_object( $tag ) && strlen( trim($element->getLabel()) ) != 0 ){
                $tag->setOption('tag', 'span' );
                //$tag->setOption('id', 'd');
            }
            else{
                $element->removeDecorator('Label');
            }

            $tag = $element->getDecorator('HtmlTag');

			if( $element->getType() == 'submit' ){
//				$element->getDecorator('')
			}

            if( is_object( $tag ) ){
                $tag->setOption('tag', 'span');
                $tag->setOption( 'class', $tag->removeOption('id') );
            }
        }

        $this->removeDecorator('HtmlTag');
    }
}
